package jasepro;

import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

// 
// Class:     Daemon
// Abstract:  Web daemon thread. creates main socket on port 8080
//            and listens on it forever. For each client request,
//            creates proxy thread to handle the request.
//
public class JasePro extends Thread {
    static ServerSocket mainSocket = null;

    public static void main(String args[]) {
        int proxyPort = 8080;

        try {
            String proxyHost = InetAddress.getLocalHost().getHostName();
            String machineNameAndPort = InetAddress.getLocalHost().getHostName() + ":" + proxyPort;
            System.out.println("Start proxy server at: " + machineNameAndPort);
            mainSocket = new ServerSocket(proxyPort);
            while (true) {
                Socket clientSocket = mainSocket.accept();
                ProxyThread thd = new ProxyThread(clientSocket, proxyHost);
                thd.start();
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        finally {
            try {
                mainSocket.close();
            } catch (Exception exc) {
            }
        }
    }
}
